<template>
  <j-modal
    :title="title"
    :width="width"
    :visible="visible"
    :confirmLoading="confirmLoading"
    switchFullscreen
    @ok="handleOk"
    @cancel="handleCancel"
    :ok-button-props="{ props: { disabled: disableSubmit } }"
    cancelText="关闭">
    <template slot="footer">
      <a-button @click="handleCancel">关闭</a-button>
      <a-button v-if="!disableSubmit" type="primary" @click="handleOk" :loading="buttonok">确定</a-button>
    </template>
    <a-spin :spinning="confirmLoading">
      <a-form :form="form">
        <a-row>

          <a-col :span="12">
            <a-form-item label="应聘者" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-search-select-tag :disabled="disableSubmit" v-decorator="['candidateId', validatorRules.candidateId]"
                                   dict="HrCandidateInfo,name,id,"/>
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-item label="公司" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-dict-select-tag
                :disabled="disableSubmit"
                type="list"
                v-decorator="['company', validatorRules.company]"
                :trigger-change="true"
                dictCode="DataDictionary,description,dictionaryCode, dictionaryTypeId =OFFER_COM_TYPE"
                placeholder="请选择职级"
              />
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-item label="部门" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-category-select :disabled="disableSubmit" v-decorator="['departmentId',validatorRules.departmentId]"
                                 pcode="{table:'HrDepartment',txt:'name',key:'departmentId',idField:'id',pidField:'parentId',hasChildField:'hasChild', condition:' parentId = 0'}"
                                 placeholder="请选择部门" back="name" @change="handleCategoryChange"/>
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-item label="职级" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-dict-select-tag
                :disabled="disableSubmit"
                type="list"
                v-decorator="['positionRank', validatorRules.positionRank]"
                :trigger-change="true"
                dictCode="DataDictionary,description,dictionaryCode, dictionaryTypeId =POSITION_RANK"
                placeholder="请选择职级"
              />
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-item label="职位" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input :disabled="disableSubmit" v-decorator="['position', validatorRules.position]"
                       placeholder="请输入职位"></a-input>
              <!-- <j-dict-select-tag :disabled="disableSubmit" type="list" v-decorator="['position', validatorRules.position]" :trigger-change="true" dictCode="DataDictionary,description,dictionaryCode, dictionaryTypeId =POSITION" placeholder="请选择职位"/> -->
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-item label="合同期(年)" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input-number :disabled="disableSubmit" v-decorator="['contractPeriod', validatorRules.contractPeriod]"
                              placeholder="请输入合同期" style="width: 100%"></a-input-number>
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-item label="试用期(月)" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input-number :disabled="disableSubmit"
                              v-decorator="['probationPeriod', validatorRules.probationPeriod]" placeholder="请输入试用期"
                              style="width: 100%"></a-input-number>
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-item label="员工类型" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-dict-select-tag :disabled="disableSubmit" type="list"
                                 v-decorator="['workingHours', validatorRules.workingHours]" :trigger-change="true"
                                 dictCode="DataDictionary,description,dictionaryCode, dictionaryTypeId = WORK_TYPE"
                                 placeholder="请选择员工类型"/>
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-item label="薪级" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-dict-select-tag @change="selectSalaryScaleId" otherField="dictionaryCode2,dictionaryCode3,dictionaryCode4,dictionaryCode5"
                                 :trigger-change="true"
                                 :disabled="disableSubmit" placeholder="请选择薪级"
                                 v-decorator="['salaryScaleId', validatorRules.salaryScaleId]"
                                 dictCode="DataDictionary,description,dictionaryCode, dictionaryTypeId = SALARY_SCALE"
                                 style="width: 100%"/>
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-item label="基薪(元)" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input onfocus="this.blur()" v-decorator="['salaryBaseId', validatorRules.salaryBaseId]" style="width: 100%"/>
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-item label="保障(元)" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input onfocus="this.blur()" v-decorator="['salaryEnsureId', validatorRules.salaryEnsureId]" style="width: 100%"/>
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-item label="月薪(元)" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input-number onfocus="this.blur()" v-decorator="['monthlySalary', validatorRules.monthlySalary]"
                              placeholder="请输入月薪" style="width: 100%"/>
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-item label="年薪(元)" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input-number onfocus="this.blur()" v-decorator="['annualSalary', validatorRules.annualSalary]"
                              placeholder="请输入年薪" style="width: 100%"/>
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-item label="新晋(元)" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input-number :disabled="disableSubmit" v-decorator="['newPersonScaleId', validatorRules.newPersonScaleId]" placeholder=""
                       style="width: 100%"/>
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-item label="其他" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input-number :disabled="disableSubmit" v-decorator="['otherDesc', validatorRules.otherDesc]" placeholder="请输入其他" style="width: 100%"/>
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-item label="预期报到日期" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-date :disabled="disableSubmit" placeholder="请选择预期报到日期"
                      v-decorator="['arrivalTime', validatorRules.arrivalTime]" :trigger-change="true"
                      style="width: 100%"/>
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-item label="有效期" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-date :disabled="disableSubmit" placeholder="请选择有效期"
                      v-decorator="['expiryDate', validatorRules.expiryDate]" :trigger-change="true"
                      style="width: 100%"/>
            </a-form-item>
          </a-col>
          <!-- <a-col :span="12">
            <a-form-item label="附件" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-upload :disabled="disableSubmit" :isMultiple="false" :fileType="pdf" v-decorator="['uploadPath']"
                        :trigger-change="true"></j-upload>
            </a-form-item>
          </a-col> -->
          <a-col :span="12">
            <a-form-item label="备注" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-textarea v-decorator="['description']" rows="4" placeholder="请输入备注" style="width: 100%"/>
            </a-form-item>
          </a-col>

          <!-- <a-col :span="12">
                  <a-form-item label="合同附件" :labelCol="labelCol" :wrapperCol="wrapperCol">
                    <j-upload :disabled="disableSubmit" :isMultiple="false" v-decorator="['uploadPath']" :trigger-change="true"></j-upload>
                  </a-form-item>
          </a-col> -->
          <a-form-item v-show="false">
            <a-input :disabled="disableSubmit" v-decorator="['name']"></a-input>
          </a-form-item>

        </a-row>
      </a-form>
    </a-spin>
  </j-modal>
</template>

<script>

  import {httpAction} from '@/api/manage'
  import pick from 'lodash.pick'
  import {validateDuplicateValue} from '@/utils/util'
  import JDate from '@/components/jeecg/JDate'
  import JUpload from '@/components/jeecg/JUpload'
  import JDictSelectTag from "@/components/dict/JDictSelectTag"
  import JSearchSelectTag from '@/components/dict/JSearchSelectTag'
  import JCategorySelect from '@/components/jeecg/JCategorySelect'

  export default {
    name: "HrOfferModal",
    components: {
      JDate,
      JUpload,
      JDictSelectTag,
      JSearchSelectTag,
      JCategorySelect,
    },
    data () {
      return {
        pdf: 'pdf',
        form: this.$form.createForm(this),
        title: "操作",
        width: 800,
        visible: false,
        buttonok: false,
        model: {},
        labelCol: {
          xs: {span: 24},
          sm: {span: 7},
        },
        wrapperCol: {
          xs: {span: 24},
          sm: {span: 16},
        },
        confirmLoading: false,
        disableSubmit: false,
        validatorRules: {
          candidateId: {
            rules: [
              {required: true, message: '请输入应聘者!'},
            ]
          },
          company: {
            rules: [
              {required: true, message: '请选择公司!'},
            ]
          },

          position: {
            rules: [
              {required: true, message: '请输入职位!'},
            ]
          },
          contractPeriod: {
            rules: [
              {required: true, message: '请输入合同期!'},
            ]
          },
          probationPeriod: {
            rules: [
              {required: true, message: '请输入试用期!'},
            ]
          },
          workingHours: {
            rules: [
              {required: true, message: '请输入员工类型!'},
            ]
          },
          annualSalary: {
            rules: [
              {required: true, message: '请输入年薪!'},
              { pattern: /(^[1-9]([0-9]+)?(\.[0-9]{1,2})?$)|(^(0){1}$)|(^[0-9]\.[0-9]([0-9])?$)/, message: '请输入正确的金额!'},
            ]
          },
          monthlySalary: {
            rules: [
              {required: true, message: '请输入月薪!'},
              { pattern: /(^[1-9]([0-9]+)?(\.[0-9]{1,2})?$)|(^(0){1}$)|(^[0-9]\.[0-9]([0-9])?$)/, message: '请输入正确的金额!'},
            ]
          },
          salaryBaseId: {
            rules: [
              { pattern: /(^[1-9]([0-9]+)?(\.[0-9]{1,2})?$)|(^(0){1}$)|(^[0-9]\.[0-9]([0-9])?$)/, message: '请输入正确的金额!'},
            ]
          },
          salaryEnsureId: {
            rules: [
              { pattern: /(^[1-9]([0-9]+)?(\.[0-9]{1,2})?$)|(^(0){1}$)|(^[0-9]\.[0-9]([0-9])?$)/, message: '请输入正确的金额!'},
            ]
          },
          newPersonScaleId: {
            rules: [
              { pattern: /(^[1-9]([0-9]+)?(\.[0-9]{1,2})?$)|(^(0){1}$)|(^[0-9]\.[0-9]([0-9])?$)/, message: '请输入正确的金额!'},
            ]
          },
          otherDesc: {
            rules: [
              { pattern: /(^[1-9]([0-9]+)?(\.[0-9]{1,2})?$)|(^(0){1}$)|(^[0-9]\.[0-9]([0-9])?$)/, message: '请输入正确的金额!'},
            ]
          },
          arrivalTime: {
            rules: [
              {required: true, message: '请输入预期报到日期!'},
            ]
          },
          expiryDate: {
            rules: [
              {required: true, message: '请选择有效期!'},
            ]
          },
          statusId: {
            rules: [
              {required: true, message: '请输入状态!'},
            ]
          },

          positionRank: {
            rules: [
              {required: true, message: '请选择部门!'},
            ]
          },
          departmentId: {
            rules: [
              {required: true, message: '请选择部门!'},
            ]
          },
          salaryScaleId: {
            rules: [
              {required: true, message: '请选择薪级!'},
            ]
          },

        },
        url: {
          add: "/online/api/createHrOffer",
          edit: "/online/api/updateHrOffer",
          get: "/online/api/getHrOffer",
        }
      }
    },
    created () {
    },
    methods: {
      add () {
        this.edit({});
      },
      edit (record) {
        this.form.resetFields();
        this.visible = true;
        this.model = {};
        if (record.id) {
          httpAction(this.url.get, {id: record.id}, "post").then((res) => {
            if (res.success) {
              this.model = Object.assign({}, res.result);
              this.$nextTick(() => {
                this.form.setFieldsValue(pick(this.model, 'candidateId','company', 'departmentId', 'positionRank','position', 'contractPeriod', 'uploadPath', 'probationPeriod', 'workingHours', 'annualSalary', 'monthlySalary', 'salaryScaleId', 'salaryBaseId', 'salaryEnsureId', 'newPersonScaleId', 'otherDesc', 'description', 'arrivalTime', 'expiryDate', 'statusId'))
              })
            }
          })
        }
      },
      close () {
        this.$emit('close');
        this.visible = false;
      },
      handleOk () {
        const that = this;
        // 触发表单验证
        this.form.validateFields((err, values) => {
          if (!err) {
            that.confirmLoading = true;
            that.buttonok = true
            let httpurl = '';
            let method = '';
            if (!this.model.id) {
              httpurl += this.url.add;
              method = 'post';
            } else {
              httpurl += this.url.edit;
              method = 'put';
            }
            let formData = Object.assign(this.model, values);
            formData.isConfirmOffer = 'N'
            formData.annualSalary = formData.annualSalary + "";
            formData.monthlySalary = formData.monthlySalary + "";
            console.log("表单提交数据", formData)
            httpAction(httpurl, formData, method).then((res) => {
              if (res.success) {
                that.$message.success('提交成功！');
                that.$emit('ok');
              } else {
                that.$message.warning('提交失败！');
              }
              setTimeout(() => {
                that.buttonok = false;
              }, 500);
            }).finally(() => {
              that.confirmLoading = false;
              that.close();
            })
          }

        })
      },
      handleCancel () {
        this.close()
      },
      popupCallback(row){
        this.form.setFieldsValue(pick(row, 'candidateId','company', 'departmentId', 'positionRank','position', 'contractPeriod', 'uploadPath', 'probationPeriod', 'workingHours', 'annualSalary', 'monthlySalary', 'salaryScaleId', 'salaryBaseId', 'salaryEnsureId', 'newPersonScaleId', 'otherDesc', 'description', 'arrivalTime', 'expiryDate', 'statusId'))
      },
      handleCategoryChange(value, backObj){
        this.form.setFieldsValue(backObj)
      },
      selectSalaryScaleId(val, row) {
        this.model.salaryBaseId = row.dictionaryCode2
        this.model.salaryEnsureId = row.dictionaryCode3
        this.model.monthlySalary = row.dictionaryCode4
        this.model.annualSalary = row.dictionaryCode5
        this.visible = true
        let fieldval = pick(this.model, 'salaryBaseId', 'salaryEnsureId','monthlySalary','annualSalary')
        this.$nextTick(() => {
          this.form.setFieldsValue(fieldval)
        })
      },


    }
  }
</script>
